// pages/user/user.  
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:null,
        showModal:false,
        isAuthention:false,
        unreadCount:0, //未读
        bigImg: '',
        phone: '15212346578'
    },
    getdeit: function(e) {
        let type = e.currentTarget.dataset.type;
        wx.navigateTo({
            url: '/pages/user/edit/edit?type='+type
        })
    },
    getpersonal: function(e) {
        let _this = this;
        wx.navigateTo({
            url: '/pages/user/personal/personal?id='+_this.data.userInfo.user_id
        })
    },
    getfollow: function(e) {
        wx.navigateTo({
            url: '/pages/user/follow/follow'
        })
    },
    getvisitrecord: function(e) {
        wx.navigateTo({
            url: '/pages/user/visitrecord/visitrecord'
        })
    },
    getparticipate: function(e) {
        wx.navigateTo({
            url: '/pages/user/participate/participate'
        })
    },
    getmemberUpgrade: function(e) {
        wx.navigateTo({
            url: '/pages/user/member_upgrade/member_upgrade'
        })
    },

    getAuthentication: function(e) {
        console.log(e.currentTarget.dataset.type);
        if(e.currentTarget.dataset.type == '系统已认证') {
            wx.navigateTo({
                url: '/pages/user/Verified/Verified'
            })
        }else {
            wx.navigateTo({
                url: '/pages/user/Authentication/Authentication'
            })
        }
        
    },
    getnews: function(e) {
        wx.navigateTo({
            url: '/pages/user/news/news'
        })
    },
    showDialogBtn: function() {
        if (!app.globalData.userInfo.user_id) {
            this.setData({
                showModal: true
            })
        } 
    },
    showBigImg: function () {
        this.setData({
            bigImg: app.globalData.userInfo.images
        })
    },
    hideBigimg: function () {
        this.setData({
            bigImg: ''
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function() {},
    /**
     * 隐藏模态对话框
     */
    hideModal: function() {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function() {
        this.hideModal();
    },
    /**
     * 对话框确认按钮点击事件
     */
    // 个人用户是否存在
    isUserExist:function(){
        let _this = this;
        wx.login({
            success:function(res){
                app.util.ajax({
                    url:'public/checkEnterpriseRegister',
                    data:{
                        code:res.code,
                        state:1
                    },
                    success:function(data){

                        if (data.status == 1) {
                            _this.loginUser();
                        }else{
                            wx.navigateTo({
                                url:'/pages/user/edit/edit?isRegister=true&type=1'
                            })
                            // _this.setData({
                            //     isAuthention:true
                            // })
                        }
                    }
                })
            }
        })
    },
    // 用户注册
    submitUserLogin:function(e){
        let _this = this,
            detail = e.detail;
        if (detail.userInfo) {
            wx.login({
                success:function(res){
                    app.util.ajax({
                        url:'/public/register',
                        data:{
                            code:res.code,
                            images:detail.userInfo.avatarUrl,
                            nickname:detail.userInfo.nickName
                        },
                        success:function(data){
                            _this.hideModal();
                            _this.loginComapny();
                            wx.setStorageSync('userInfo',data);
                            _this.setData({
                                isAuthention:false,
                                userInfo:data
                            })
                        }
                    })
                }
            })
        }
    },
    // 用户登录
    loginUser: function(event) {
        let _this = this;
        wx.login({
            success:function(res){
                app.util.ajax({
                    url:'Public/login',
                    data:{
                        state:1,
                        code:res.code
                    },
                    success:function(data){
                        _this.hideModal();
                        app.globalData.userInfo = data;
                        wx.setStorageSync('userInfo',data);
                        _this.setData({
                            userInfo:data
                        })
                        _this.getUnread();
                    }
                })
            }
        })
    },
    // 企业是否注册
    isCompanyExist:function(){
        let _this = this;
        new Promise(function(resolve,reject){
            wx.login({
                success:function(res){
                    resolve(res.code);
                }
            })
        })
        .then( (code) => {
            return new Promise(function(resolve,reject){
                app.util.ajax({
                    url:'public/checkEnterpriseRegister',
                    data:{
                        code:code,
                        state:2
                    },
                    success:function(res){
                        if (res.status == 1) {
                            _this.loginComapny();
                            _this.setData({
                                showModal:false
                            })
                        }else{
                            wx.navigateTo({
                                url:'/pages/user/edit/edit?isRegister=true&type=2'
                            })
                        }
                    }
                })
            })
                
        })
    },
    // 企业登录
    loginComapny:function(){
        let _this = this;
        wx.login({
            success:function(res){
                app.util.ajax({
                    url:'Public/login',
                    data:{
                        state:2,
                        code:res.code,
                        images:'',
                        nickname:''
                    },
                    success:(data) => {
                        app.globalData.userInfo = data;
                        wx.setStorageSync('userInfo',data);
                        _this.setData({
                            userInfo:data
                        });
                    }
                })
            }
        })    
    },
    // 获取未读消息
    getUnread:function(){
        let _this = this;
        app.util.ajax({
            url:'release/getNoReadCount',
            data:{
                user_id:app.globalData.userInfo.user_id
            },
            success:function(res){
                _this.setData({
                    unreadCount:res.count
                })
            }
        })
        // 获取电话
        app.util.ajax({
            url:'con/contact',
            success:function(res){
                _this.setData({
                    phone: res.data
                })
            }
        })
    },
    // 拨打电话
    call: function () {
        wx.makePhoneCall({
            phoneNumber: this.data.phone,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (!app.globalData.userInfo) {
            this.isUserExist();
            // this.setData({
            //     showModal:true
            // })
            // return;
        } else {
            this.getUnread();
        }
        // if (wx.getStorageSync('userInfo')) {
        //     this.setData({
        //         showModal:false
        //     })
        // }
        this.setData({
            userInfo:wx.getStorageSync('userInfo') || null,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // 分享
    onShareAppMessage(res) {
        return {
            title: '广州大学校友会校友企业家专委会',
            path: '/pages/index/index'
        }
    },

})